Lecture 5 (Part 1) - Practice

Q1: Population Density

اعمل قاعدة بيانات للسكان والمساحة للدول دي:

Country Population (in million people) Area (in millions of square miles)
Usa 203 3
India 548 1
China 800 4
Brazil 108 3

وبعدين اعمل rule: ا density is population divided by area.

واكتب الاستعلامات:

  1. Find the population of India.
  2. Find the area of Brazil.
  3. Find the population density of China.

Q2: Arithmetic Functions

اعمل rules للدوال الرياضية دي:

  1. Sum: S = X + Y
  2. Calculate: y = (x + 3)²
  3. Polynomial: F = X⁴ + 2X + 5

واكتب الاستعلامات:


Q3: Factorial

اعمل recursive rule لحساب factorial:

واكتب الاستعلام:


Q4: GCD

اعمل recursive rule لحساب greatest common divisor (GCD):

  1. If X and Y are equal, D = X.
  2. If X < Y, D = gcd(X, Y - X).
  3. If Y < X, D = gcd(Y, X).

واكتب الاستعلامات:


Q5: Logic Gates

اعمل Facts للبوابات المنطقية الأساسية (not, and, or, xor):

Inputs and or xor
0, 0 0 0 0
0, 1 0 1 1
1, 0 0 1 1
1, 1 1 1 0
Inputs not
0 1
1 0

Q6: Half Adder

استخدم "and" و "xor" من Q5 عشان تبني half-adder:

وجرب inputs (0,1) و (1,1).


Q7: Boolean Function

استخدم "not", "and", "or" من Q5 عشان تعمل rule للدالة:
F = B' + AC

يعني: not B, then and(A, C), then or the results.

وجرب inputs:


Q8: Summation 1 to X

اعمل recursive rule تجمع الأرقام من 1 لأي رقم X:
F = 1 + 2 + 3 + ... + X

Base: sum_to(1, 1).
Recursive: sum_to(X, S) :- sum_to(X-1, S1), S is S1 + X.

واكتب الاستعلامات:


Q9: Summation of Odd Numbers 1 to M

اعمل recursive rule تجمع الأرقام الفردية (odd) من 1 لأي رقم فردي M:

Base: sum_odd(1, 1).

واكتب الاستعلامات:


Nour Eldeen Mahmoud


Powered by Forestry.md